package com.gopay.scheduler.job;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.gopay.common.constants.oldmermove.OldMerTranEffectStatus;
import com.gopay.common.domain.oldmermove.OldMovedMer;
import com.gopay.common.domain.oldmermove.OldMovedMerOpenTran;
import com.gopay.common.oldmermove.dao.OldMovedMerDAO;
import com.gopay.common.oldmermove.dao.OldMovedMerOpenTranDAO;
import com.gopay.scheduler.GopayBaseJob;

/**
 * 旧平台商户迁移交易开通，置为生效状态定时任务
 * 
 * @OldMerMoveTranOpenEffectJob.java
 * @author zhangjunyu
 * @2014-9-23 下午5:27:37 www.gopay.com.cn Inc.All rights reserved.
 */
public class OldMerMoveTranOpenEffectJob extends GopayBaseJob {

	private Logger logger = LoggerFactory.getLogger(OldMerMoveTranOpenEffectJob.class);
	
	@Autowired
	private OldMovedMerDAO oldMovedMerDAO;
	/** 旧平台迁移商户交易开通dao */
	@Autowired
	private OldMovedMerOpenTranDAO oldMovedMerOpenTranDAO;

	@Override
	public void doJobInternal() {
		
		List<OldMovedMerOpenTran> tranList = oldMovedMerOpenTranDAO.findSetupEffectTimeLessNow();
		logger.info("List<OldMovedMerOpenTran>.size={}", tranList.size());

		Map<String, OldMovedMer> effectMerMap = new HashMap<String, OldMovedMer>();
		
		for (OldMovedMerOpenTran tran : tranList) {
			tran.setEffectStatus(OldMerTranEffectStatus.HAVE_EFFECT);
			logger.info("update Tran EffectStatus,merId={},tranCode={}", tran.getId().getMerId(), tran.getId()
					.getTranCode());
			oldMovedMerOpenTranDAO.update(tran);
			
			String merId = tran.getId().getMerId();
			if(!effectMerMap.containsKey(merId)) {
				OldMovedMer mer = oldMovedMerDAO.getByOldMerId(merId);
				effectMerMap.put(merId, mer);
			}
		}
		
		/* 更新OldMovedMer对象中交易设置生效状态 */
		oldMovedMerDAO.updateTranEffectStatusByMerSet(effectMerMap.values(), OldMerTranEffectStatus.HAVE_EFFECT);
		

		
	}

}
